package com.example.sens.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.example.sens.entity.*;
import com.example.sens.service.ThresholdService;
import com.example.sens.utils.Response;
import com.example.sens.vo.ThresholdVo;
import com.example.sens.vo.UserQuizVo;
import com.example.sens.vo.UserThresholdSaveParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api/threshold")
@Slf4j
public class ThresholdController {

    @Resource
    private ThresholdService thresholdService;

    @PostMapping("find")
    @ResponseBody
    public Response<List<ThresholdCustom>> find(@RequestBody ThresholdVo thresholdVo) {
        return thresholdService.find(thresholdVo);
    }

    @PostMapping("save")
    @ResponseBody
    public Response save(@RequestBody UserThresholdSaveParam param,HttpSession session) throws Exception {
        return thresholdService.save(param,session);
    }

    @PostMapping("getPersonData")
    @ResponseBody
    public Response<Page<List<UserQuiz>>> getByPage(@RequestBody UserQuizVo userQuizVo, HttpSession session) {
        Page<List<UserQuiz>> quizPage = null;
        try {
            User user = (User)session.getAttribute("user");
            quizPage = thresholdService.findAll(new Page(userQuizVo.getPageNo(), userQuizVo.getPageSize()),user.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return Response.no();
        }
        return Response.yes(quizPage);
    }


    @PostMapping("getAllData")
    @ResponseBody
    public Response<Page<List<UserDataCustom>>> getAllData(@RequestBody UserQuizVo userQuizVo) {
        Page<List<UserDataCustom>> quizPage = null;
        try {
            quizPage = thresholdService.findAll(new Page(userQuizVo.getPageNo(), userQuizVo.getPageSize()));
        } catch (Exception e) {
            e.printStackTrace();
            return Response.no();
        }
        return Response.yes(quizPage);
    }
}
